﻿#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"


	Date::Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date::~Date()
	{
		_year = _month = _day = 0;
	}
	Date::Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	//d1==d2;
	bool Date::operator==(const Date& d)const
	{
		return _year == d._year && _month == d._month && _day == d._day;
	}
	//d1=d2;d1=d1;d3=d1=d2;
	Date& Date::operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}

	bool Date::operator!=(const Date& d)const
	{
		return !(*this==d);
	}
	//d1>d2
	bool Date::operator>(const Date& d)const
	{
		if (_year > d._year)
		{
			return true;
		}
		else if (_year == d._year && _month > d._month)
		{
			return true;
		}
		else if (_year == d._year && _month == d._month && _day > d._day)
		{
			return true;
		}
		return false;
	}
	//d1>=d2
	bool Date::operator>=(const Date& d)const
	{
		return (*this > d) || (*this == d);
	}
	bool Date::operator<(const Date& d)const
	{
		return !(*this >= d);
	}
	bool Date::operator<=(const Date& d)const
	{
		return (*this < d) || (*this == d);
	}

	//d1+50
	Date Date::operator+(int day)
	{
		Date tmp(*this);
		tmp._day += day;
		while (tmp._day > Getmonthday(tmp._year, tmp._month))
		{
			tmp._day -= Getmonthday(tmp._year, tmp._month);
			tmp._month++;
			if(tmp._month == 13)
			{
				tmp._month = 1;
				tmp._year++;
			}
		}
		return tmp;
	}



	////d1=d1+50
	Date& Date::operator+=(int day)
	{
		*this=*this + day;
		return *this;
	}


	//d1-50
	Date Date::operator-(int day)
	{
		Date tmp(*this);
		tmp._day -= day;
		while (tmp._day <= 0)
		{
			tmp._day += Getmonthday(tmp._year, tmp._month);
			tmp._month--;
			if (tmp._month == 0)
			{
				tmp._year--;
				tmp._month = 12;
			}
			tmp._day = Getmonthday(tmp._year, tmp._month);
		}
		return tmp;
	}
	Date& Date::operator-=(int day)
	{
		*this = *this - day;
		return *this;
	}
	
	//++d1
	Date& Date::operator++()
	{
		*this += 1;
		return *this;
	}
	//d1++
	Date Date::operator++(int)
	{
		Date tmp(*this);
		*this += 1;
		return tmp;
	}
	//d1-d2
	int	Date::operator-(const Date d)
	{
		//���������С
		int flag = 1;
		Date max = *this;
		Date min = d;
		//���˾�����С�Ҵ�
		if (*this < d)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int day = 0;
		while (min != max)
		{
			min++;
			day++;
		}
		return day*flag;
		
	}

	Date* Date::operator&()
	{
		return this;
	}
	const Date* Date::operator&()const
	{
		return this;
	}

	ostream& operator<<(ostream& out, const Date& d)
	{
		out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
		return out;
	}
	istream& operator>>(istream& in, Date& d)
	{
		 in >> d._year >> d._month >> d._day;
		 return in;
	}



	int Date::Getmonthday(int year,int month)
	{
		assert(year > 0 && month < 13 && month>0);
		int montharr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && (year % 4 == 0 || year % 100 != 0) || (year % 400 == 0))
		{
			return 29;
		}
		return montharr[month];
	}

